package com.feeyin.radar.msg.controller;

import com.feeyin.radar.commons.DatePropertyEditor;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.model.Message;
import com.feeyin.radar.msg.service.NewsNoticeService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;

public class NewsNoticeController extends MultiActionController
{
  private NewsNoticeService newsNoticeService;
  private String newsNotice;
  private String addNewsNotice;
  private String updateNewsNotice;
  private String viewNewsNotice;

  public NewsNoticeService getNewsNoticeService()
  {
    return this.newsNoticeService;
  }

  public void setNewsNoticeService(NewsNoticeService newsNoticeService) {
    this.newsNoticeService = newsNoticeService;
  }

  public String getNewsNotice() {
    return this.newsNotice;
  }

  public void setNewsNotice(String newsNotice) {
    this.newsNotice = newsNotice;
  }

  public String getAddNewsNotice() {
    return this.addNewsNotice;
  }

  public void setAddNewsNotice(String addNewsNotice) {
    this.addNewsNotice = addNewsNotice;
  }

  public String getUpdateNewsNotice() {
    return this.updateNewsNotice;
  }

  public void setUpdateNewsNotice(String updateNewsNotice) {
    this.updateNewsNotice = updateNewsNotice;
  }

  public String getViewNewsNotice() {
    return this.viewNewsNotice;
  }

  public void setViewNewsNotice(String viewNewsNotice) {
    this.viewNewsNotice = viewNewsNotice;
  }

  public ModelAndView display(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));

    String sign = (String)request.getSession().getAttribute("isManageNewsNotice");

    String sFlag = request.getParameter("sFlag");
    String subject = "";
    if ((!StringUtils.isEmpty(sFlag)) && ("1".equals(sFlag))) {
      subject = request.getParameter("subject");
    }
    model.put("sFlag", sFlag);
    model.put("subject", subject);

    Page page = new Page();

    page.getPageList(this.newsNoticeService.getPageNewsNoticesCount(subject).intValue(), null, request);
    model.put("newsNotices", this.newsNoticeService.getPageNewsNotices(page, subject));
    return new ModelAndView(getNewsNotice(), model);
  }

  public ModelAndView preAddNewsNotice(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    return new ModelAndView(getAddNewsNotice(), model);
  }

  public ModelAndView addNewsNotice(HttpServletRequest request, HttpServletResponse response, Message message)
  {
    boolean result = false;
    int success = 0;
    if (message != null) {
      result = this.newsNoticeService.insertNewsNotice(message);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./newsNotice.do?action=preAddNewsNotice&result=" + success));
  }

  public ModelAndView preUpdateNewsNotice(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    model.put("result", request.getParameter("result"));
    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(id)) {
      model.put("newsNotice", this.newsNoticeService.getNewsNoticeById(Integer.valueOf(Integer.parseInt(id))));
    }
    return new ModelAndView(getUpdateNewsNotice(), model);
  }

  public ModelAndView updateNewsNotice(HttpServletRequest request, HttpServletResponse response, Message message)
  {
    boolean result = false;
    int success = 0;
    if (message != null) {
      result = this.newsNoticeService.updateNewsNotice(message);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./newsNotice.do?action=preUpdateNewsNotice&result=" + success + "&id=" + message.getId()));
  }

  public ModelAndView deleteNewsNotice(HttpServletRequest request, HttpServletResponse response)
  {
    boolean result = false;
    int success = 0;
    String[] ids = request.getParameterValues("id");
    if ((ids != null) && (ids.length > 0)) {
      result = this.newsNoticeService.deleteNewsNotice(ids);
      if (result) success = 1;
    }
    return new ModelAndView(new RedirectView("./newsNotice.do?action=display&result=" + success));
  }

  public ModelAndView viewNewsNotice(HttpServletRequest request, HttpServletResponse response)
  {
    Map model = new HashMap();
    String id = request.getParameter("id");
    if (!StringUtils.isEmpty(id)) {
      Message newsNotice = new Message();
      newsNotice = this.newsNoticeService.getNewsNoticeById(Integer.valueOf(Integer.parseInt(id)));
      model.put("newsNotice", newsNotice);
    }

    return new ModelAndView(getViewNewsNotice(), model);
  }

  protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception
  {
    binder.registerCustomEditor(Date.class, new DatePropertyEditor());
  }
}